import jwt from 'jsonwebtoken'
import bcrypt from 'bcrypt'

export type JwtPayload = { userId: number }

export function signJwt(userId: number) {
  const config = useRuntimeConfig()
  return jwt.sign({ userId } as JwtPayload, config.jwtSecret, { expiresIn: '7d' })
}

export function verifyJwt(token: string): JwtPayload | null {
  try {
    const config = useRuntimeConfig()
    return jwt.verify(token, config.jwtSecret) as JwtPayload
  } catch {
    return null
  }
}

export async function hashPassword(password: string) {
  const saltRounds = 10
  return bcrypt.hash(password, saltRounds)
}

export async function comparePassword(password: string, hash: string) {
  return bcrypt.compare(password, hash)
}


